home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / yase.arc / EDIT1.ASM < prev    next >
Encoding:
Assembly Source File  |  1986-12-13  |  3.9 KB  |  114 lines

  1. ******************************************************************
  2. * COPYRIGHT (C) 1986 by Donald Krantz and James Stanley
  3. * - Note: This is a real, live, actual, registered copyright,
  4. *   and should be treated as such. This source code is from
  5. *   the book "68000 Assembly Language", Krantz and Stanley,
  6. *   Addison-Wesley Publishing Company, Reading, MA, 1986.
  7. *   Permission granted by the authors for non-commercial use
  8. *   in programs released to the public domain, as long as this
  9. *   copyright notice remains attached and visible.
  10. *
  11. *****************************************************************
  12. * EDIT1 - Editor file I/O routines.
  13. *
  14. * Routines:    READ_IN    Reads a file into an editing buffer.
  15. *         Entry:    Register A5.L points to an initialized
  16. *            edit buffer descriptor block.
  17. *            Register A0.L points to filename string
  18. *         Exit:    ed_err(a5) set if any problems.
  19. *
  20. *        WRITE_OUT writes a block of memory data to a
  21. *            file. 
  22. *         Entry:    Register A0.L points to filename string.
  23. *            Register A1.L points to the start of data
  24. *            area in memory. D1.W holds number of 
  25. *            bytes to write into file.
  26. *        Exit:    ed_err(a5) set if any problems.
  27.  
  28.     .xref     open,close,fgetc,fputc
  29.     .xdef    read_in,write_out
  30.  
  31. #fcb.h
  32. #edit.h
  33.  
  34. *****************************************************************
  35. * READ_IN - Reads a file into an editing buffer.
  36. read_in:
  37.     movem.l    d1/a0/a1,-(a7)    * Save caller's registers.
  38.     link    a6,#-file    * Create an FCB on the stack
  39.     move.l    a7,a1        * get FCB address
  40.     move.w    #0,-(a7)    * Push 0 for "File Must Exist"
  41.     move.l    a0,-(a7)    * Push filename
  42.     move.l    a1,-(a7)    * Push FCB address
  43.     bsr    open        * Attempt open
  44.     tst.w    d0        * Did it work?
  45.     bpl    sk1_read    * Yes.
  46.     move.w    #1,d0        * Show "File-not-found" error
  47.     bra    read_exit    * Say goodbye.
  48. sk1_read:
  49.     move.l    e_gap(a5),d1    * Compute buffer length
  50.     sub.l    b_gap(a5),d1    
  51.     move.l    b_gap(a5),a1    * Setup pointer to data area
  52.     bra    sk3_read    * Loop test done before read
  53. lp1_read:
  54.     bsr    fgetc        * Read character
  55.     tst.w    d0        * Look for end of file
  56.     bmi    sk2_read    * Yes, found end-of-file
  57.     cmp.b    #13,d0        * is this C/R?
  58.     beq    lp1_read    * yes, just skip it.
  59.     move.b    d0,(a1)+    * Store datum
  60. sk3_read:
  61.     dbra    d1,lp1_read    * Go for the next one.
  62.     move.w    #2,d0        * If we're here, file's too big.
  63.     bra    read_exit    * So leave.
  64. sk2_read:
  65.     move.l    a1,b_gap(a5)    * save in descriptor structure
  66.     move.w    #0,d0        * Show successful read.
  67. read_exit:
  68.     unlk    a6        * Restore frame pointer
  69.     movem.l    (a7)+,d1/a0/a1    * Restore caller's registers.
  70.     rts
  71. *****************************************************************
  72. * WRITE_OUT - writes a file to disk
  73. write_out:
  74.     movem.l    d1/a0-a2,-(a7)    * Save caller's registers.
  75.     link    a6,#-file    * Create an FCB on the stack
  76.     move.l    a7,a2        * Get FCB address
  77.     move.w    #$FFFF,-(a7)    * Push -1 for "Create File"
  78.     move.l    a0,-(a7)    * Push filename
  79.     move.l    a2,-(a7)    * Push FCB address
  80.     bsr    open        * Attempt open
  81.     tst.w    d0        * Did it work?
  82.     bpl    sk4_write    * Yes, loop test before write.
  83.     move.w    #1,d0        * Show "Can't make file" error
  84.     bra    write_exit    * Say goodbye.
  85. lp1_write:
  86.     cmp.b    #10,(a1)    * Is this a newline?
  87.     bne    sk3_write    * No.
  88.     move.b    #13,5(a7)    * Store RETURN into stack
  89.     bsr    fputc        * write RETURN.
  90.     tst.w    d0        * Look for error.
  91.     bmi    sk2_write    * Yes, found error.
  92. sk3_write:
  93.     move.b    (a1)+,5(a7)    * Store parameter into stack
  94.     bsr    fputc        * write character
  95.     tst.w    d0        * Look for error.
  96.     bmi    sk2_write    * Yes, found error.
  97. sk4_write:
  98.     dbra    d1,lp1_write    * Go for the next one.
  99.     move.l    a2,-(a7)    * push FCB address
  100.     bsr    close        * close file
  101.     tst.w    d0        * smooth return code
  102.     bmi    write_exit    * error exit
  103.     clr.w    d0        * show success
  104.     bra    write_exit    * and get out of here.
  105. sk2_write:
  106.     move.w    #1,d0        * Show error.
  107. write_exit:
  108.     unlk    a6        * Restore frame pointer
  109.     movem.l    (a7)+,d1/a0-a2    * Restore caller's registers.
  110.     rts
  111.  
  112.     end
  113.